<template>
    <section>

        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">

                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="用户名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select clearable v-model="query.state" placeholder="用户状态">
                        <el-option label="待审核" value="0"></el-option>
                        <el-option label="通过" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>


    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="用户名称" width="120" sortable>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="100" sortable>
        </el-table-column>
        <el-table-column prop="age" label="宠物年纪" width="100" sortable>
        </el-table-column>
        <el-table-column prop="gender" label="宠物性别" width="100" sortable>
        </el-table-column>
        <el-table-column prop="coat_color" label="毛色" width="100" sortable>
        </el-table-column>
        <el-table-column prop="resource" label="资源" width="100" sortable>
        </el-table-column>
        <el-table-column prop="pet_type" label="宠物类型" width="100" sortable>
        </el-table-column>
        <el-table-column prop="address" label="地址" width="100" sortable>
        </el-table-column>

        <el-table-column prop="state" label="状态" width="120">
            <template scope="scope">
                <span v-if="scope.row.state==0" style="color: #8c939d">待审核</span>
                <span v-if="scope.row.state==1" style="color: #13ce66">审核通过</span>
                <span v-if="scope.row.state==-1" style="color: #ff4d51">驳回</span>
            </template>

        </el-table-column>

        <el-table-column prop="title" label="品种" width="100" sortable>
        </el-table-column>
        <el-table-column prop="user_id" label="用户id" width="100" sortable>
        </el-table-column>

        <el-table-column prop="shop_id" label="店铺id" width="100" sortable>
        </el-table-column>

        <el-table-column label="操作" width="150">
            <template scope="scope">
                <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="用户名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="宠物价格" prop="price">
                    <el-input v-model="addForm.price" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="宠物年纪" prop="age">
                    <el-input v-model="addForm.age" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="毛色" prop="coat_color">
                    <el-input v-model="addForm.coat_color" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="资源" prop="resources">
                    <el-input v-model="addForm.resources" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="pet_type">
                    <el-input v-model="addForm.pet_type" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="addForm.address" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="状态" prop="state">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="0">待审核</el-radio>
                    </el-radio-group>
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">审核通过</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="品种" prop="title">
                    <el-input v-model="addForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="用户id" prop="user_id">
                    <el-input v-model="addForm.user_id" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="店铺id" prop="shop_id">
                    <el-input v-model="addForm.shop_id" auto-complete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>



        </section>
</template>

<script>
    export default {

        data(){
            return{
                listLoading:false,
                listData:[],
                sels:[],
                total:0,
                query:{
                    pageSize:5,
                    currentPage:1,
                    keywords:''
                },
                addFormVisible:false,//编辑界面是否显示出来
                addLoading:false,// 避免操作数据加载不出来卡住
                addFormRules:{

                    name: [
                        { required: true, message: '用户名称', trigger: 'name' }
                    ],
                    state: [
                        { required: true, message: '状态', trigger: 'state' }
                    ]
                },
                addForm:{
                    name: '',

                    price:null,
                    age:null,
                    gender:null,
                    coat_color:'',
                    resources:'',
                    pet_type:null,
                    address:'',
                    state:0,
                    title:'',
                    user_id:null,
                    shop_id:null
                },

            }
        },
        methods:{
            // 显示全部数据
            loadListData(){
                // 将查询到的数据都展示出来
                this.$http.post("/searchMasterMsg",this.query).then((res)=>{
                    console.log(res);
                    // 赋值的过程
                    this.listData=res.data.data;
                    this.total =res.data.total;

                })

            },
            selsChange(val){
                console.log(val)
               // sels只要有数据就能点击
                this.sels=val;
            },
            formatSex: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
            },
            // 批量删除
            batchRemove(){
                // 遍历获取到的ids的一个数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送patch请求需要传递参数，将上面取出来的ids数组对象传入
                    this.$http.patch("/searchMasterMsg",ids).then(res=>{
                        // 结构表达式取出data中的参数，用来做提示用语
                        let{success,message}=res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else {
                            this.$message({
                                type: 'info',
                                message: '删除失败，操作异常'
                            });
                        }
                        // 不管删除失败还是成功都要刷新页面
                        this.loadListData();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });




            },
            handleCurrentChange(val){
                console.log(val)
                this.query.currentPage=val;
                this.loadListData();

            },
            // 编辑方法
            handleEdit(index,row){
                this.addFormVisible = true;
                this.addForm = Object.assign({}, row);

            },
            // 高级查询方法
            search(){
                // 初始化当前页
                this.query.currentPage=1;
                this.loadListData();
            },
            // 新增方法
            handleAdd(){
                this.addForm={
                    name: '',
                    price:null,
                    age:null,
                    gender:null,
                    coat_color:'',
                    resources:'',
                    pet_type:null,
                    address:'',
                    state:0,
                    title:'',
                    user_id:null,
                    shop_id:null

                }
                this.addFormVisible = true;


            },
            handleDel(index,row){

                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 把row对象中的id取出来
                     let id = row.id;
                     // 字符串拼接id，http://localhost:8080/dept/9,dept后面还有一个/和拼接了id
                     this.$http.delete("/searchMasterMsg/"+id).then(res=>{
                           let{success,message}=res.data;
                                if(success){
                                    this.$message({
                                        type: 'success',
                                        message: '删除成功!'
                                    });

                                }else {
                                    this.$message({
                                        type: 'info',
                                        message: '删除失败，操作异常'
                                    });
                                }
                                    // 不管删除失败还是成功都要刷新页面
                                    this.loadListData();
                             })


                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });

            },
            addSubmit(){
                // 开启添加框
                this.addFormVisible = true;
                this.$http.put("/searchMasterMsg",this.addForm).then(res=>{
                    // 解构 拿出两个参数
                    let{success,message}=res.data;
                    if(success){
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        });

                    }else {
                        this.$message({
                            type: 'info',
                            message: '操作失败，操作异常'
                        });
                    }
                    // 不管删除失败还是成功都要刷新页面
                    this.addFormVisible=false;
                    this.loadListData();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '保存失败'
                    });
                });

            }


        },

        mounted(){

        this.loadListData();

        }



    }

</script>

<style >

</style>